﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using myFirtz.Interface;
using System.Threading.Tasks;
using System.Web.Services.Description;
using System.Web.UI.HtmlControls;

namespace myFritz.GUI
{
    public partial class _Default : Page
    {
        Session _session;
        DeviceList _deviceList;


        private void GetDevices()
        {
            _session.Connect("xxx", "xxx", "fritz.box");
            _deviceList = _session.GetDeviceListInfos();

            if (_deviceList.devicelist.Count() == 0)
            {
                _deviceList.LoadExample();
            }

            ListView_Device.DataSource = _deviceList.devicelist;
            ListView_Device.DataBind();
        }

        private Device GetDeviceFromHtmlBtn(object sender)
        {
            HtmlButton btn = (HtmlButton)sender;
            string id = btn.Attributes["itemid"].ToString();
            Device selectedDevice = _deviceList.devicelist.Where(p => p.Id.Equals(id)).FirstOrDefault();
            return (selectedDevice);
        }

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            _session = new Session();
            GetDevices();
        }

        protected void OnOffBtn_Click(object sender, EventArgs e)
        {
            Device selectedDevice = GetDeviceFromHtmlBtn(sender);
            _session.SetSwitchToggle(selectedDevice);
            GetDevices();
        }

        protected void PlusBtn_Click(object sender, EventArgs e)
        {
            Device selectedDevice = GetDeviceFromHtmlBtn(sender);
            _session.SetHKRtSoll(selectedDevice, (Convert.ToDouble(selectedDevice.HKR.Tist) + 0.5).ToString());
            GetDevices();
        }

        protected void MinusBtn_Click(object sender, EventArgs e)
        {
            Device selectedDevice = GetDeviceFromHtmlBtn(sender);
            _session.SetHKRtSoll(selectedDevice, (Convert.ToDouble(selectedDevice.HKR.Tist) - 0.5).ToString());
            GetDevices();
        }

        #endregion
    }
}